<?php

namespace App\Models;

use App\Traits\GetByOpenid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\MemberAccount
 *
 * @property int $id
 * @property string $openid
 * @property float $balance
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @property string|null $deleted_at
 * @property-read \App\Models\Member $member
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\MemberAccount onlyTrashed()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberAccount whereBalance($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberAccount whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberAccount whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberAccount whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberAccount whereOpenid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\MemberAccount whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\MemberAccount withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\MemberAccount withoutTrashed()
 * @mixin \Eloquent
 */
class MemberAccount extends Model
{
    use GetByOpenid, SoftDeletes;

    protected $fillable = ['openid'];

    public function member()
    {
        return $this->hasOne(Member::class, 'openid', 'openid');
    }

    public static function getMemberAccountByOpenid(string $openid)
    {
        $model = self::getByOpenid($openid);
        empty($model->balance) && $model->balance = 0;
        return $model;
    }
}
